package com.exrobot.module.rbt.mq.consumer.prepareFood;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.exrobot.module.rbt.controller.admin.order.vo.OrderRespVO;
import com.exrobot.module.rbt.controller.admin.order.vo.OrderSaveReqVO;
import com.exrobot.module.rbt.controller.admin.orderitem.vo.OrderItemRespVO;
import com.exrobot.module.rbt.controller.admin.robottask.vo.RobotTaskSaveReqVO;
import com.exrobot.module.rbt.controller.app.vo.MealCodeVO;
import com.exrobot.module.rbt.dal.dataobject.order.OrderDO;
import com.exrobot.module.rbt.dal.dataobject.orderitem.OrderItemDO;
import com.exrobot.module.rbt.dal.dataobject.robottask.RobotTaskDO;
import com.exrobot.module.rbt.enums.RbtOrderStatusEnum;
import com.exrobot.module.rbt.enums.RbtTaskStatusEnum;
import com.exrobot.module.rbt.enums.RbtTaskTypeEnum;
import com.exrobot.module.rbt.framework.util.CommonUtils;
import com.exrobot.module.rbt.mq.message.prepareFood.PrepareFoodMessage;
import com.exrobot.module.rbt.service.mqtt.MqttMessageService;
import com.exrobot.module.rbt.service.order.OrderService;
import com.exrobot.module.rbt.service.robotScheduler.RobotSchedulerService;
import com.exrobot.module.rbt.service.robottask.RobotTaskService;
import jakarta.annotation.Resource;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

import static com.exrobot.framework.common.util.collection.CollectionUtils.convertList;

/**
 * 针对 {@link JSONObject} 的消费者
 *
 * @author wf
 */
@Component
@Slf4j
public class PrepareFoodSendConsumer {
    @Resource
    private OrderService orderService;

    @Resource
    private RobotTaskService taskService;

    @Resource
    private MqttMessageService mqttMessageService;

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(PrepareFoodMessage prepareFoodMessage) {
        log.info("[RedisMQ-配餐]接收消息内容：{}", JSON.toJSONString(prepareFoodMessage));
        List<OrderItemDO> list = convertList(prepareFoodMessage.getData(), item -> {
            return new OrderItemDO()
                    .setRobotSn(prepareFoodMessage.getSn())
                    .setOrderSn(prepareFoodMessage.getOrderSn())
                    .setRobotBoxSn(item.getBoxNo())
                    .setCanteenSn(item.getCanteenId())
                    .setMenuSn(item.getFoodId())
                    .setCreateTime(LocalDateTime.now())
                    .setStatus(RbtOrderStatusEnum.WAIT.getVal())
                    .setMealTimes(CommonUtils.getMealTime(LocalDateTime.now()));

        });
        OrderSaveReqVO reqVO = new OrderSaveReqVO();
        reqVO.setItemDOList(list);
        if (StringUtil.isEmpty(prepareFoodMessage.getOrderSn())) {
            orderService.insertBatchOrderItem(list);
        //schedulerService.processCanteenOrder(prepareFoodMessage.getSn(),list);
        } else {
            reqVO.setOrderSn(prepareFoodMessage.getOrderSn());
            //1、更新订单中的机器人和餐盒信息
            OrderDO orderDO = orderService.updateOrderBindRobotbox(reqVO);
            if(orderDO == null) {
                return;
            }
            //2、添加送餐任务
            RobotTaskSaveReqVO saveReqVO = new RobotTaskSaveReqVO();
            saveReqVO.setTaskType(RbtTaskTypeEnum.DELIVERY.getTypeVal());
            saveReqVO.setStatus(RbtTaskStatusEnum.DISPATCH.getVal());
            saveReqVO.setPoint(orderDO.getPoint());
            saveReqVO.setRobotSn(prepareFoodMessage.getSn());
            RobotTaskDO taskDO = taskService.createRobotTask(saveReqVO);
            if(taskDO == null) {
                return;
            }
            //3、给机器人发信息送餐消息
            MealCodeVO mealCodeVO = new MealCodeVO()
                    .setTaskId(taskDO.getTaskNo())
                    .setSiteId(orderDO.getPoint())
                    .setCode(orderDO.getCheckCode())
                    .setBoxNos(list.stream().map(OrderItemDO::getRobotBoxSn).toList());
            mqttMessageService.sendMealCode(prepareFoodMessage.getSn(),mealCodeVO);
        }
    }

}
